var Contact,Vacancy,Profile,Duration,rate,cDate,PriorityDate,FinishDate,SalesPerson,Client,noVacancy,experience,request,residence,lenguage,travel;

function loadSolicitudPage() {
	comboDrillDown(1);
}

function comboDrillDown(typeCombo) {
	var idClient = 0;
	var idVendedor = 0;
	if(typeCombo=="2") {
		idVendedor = $("#sdSalesperson_1 .combo option:selected").attr("id");
	} else 
		if(typeCombo=="3"){
			idClient = $("#sdClient_1 .combo option:selected").attr("id");
		}
	/***/
	var params = {
					process	 	: "loadSelects",
					type		: typeCombo,
					idSales		: idVendedor,
					idCliente 	: idClient 
				 };
	var jsonReq = ajaxJSONtoPOST(Object.toJSON(params), serviceDemand);
	if(jsonReq.Flag = "true") {
		if(typeCombo=="1") { $("#sdSalesperson_1 .lblcombo").html(jsonReq.combo); 	}
		else
		if(typeCombo=="2") { $("#sdClient_1 .lblcombo").html(jsonReq.combo); 	}
		else
		if(typeCombo=="3") { $("#sdContact_1 .lblcombo").html(jsonReq.combo); 	}
	}
	/***/
}

function saveDemand() {
	/****/
	var tipo = $("#demandSelected").val()==""?1:2;
	if(fieldsAreValids()) {

		/***/
		var jsonObject = {
				process 		: "cDemand",
				type			: tipo,
				vacancy 		: Vacancy,
				profile 		: Profile,
				creationDate	: cDate,
				duration		: Duration,
				rate			: Rate,
				contact 		: $("#sdContact_1 .lblcombo option:selected").attr("id"),
				salesperson		: $("#sdSalesperson_1 .lblcombo option:selected").attr("id"),
				client			: $("#sdClient_1 .lblcombo option:selected").attr("id"),
				novacancy		: noVacancy,
				idDemand		: $("#demandSelected").val()==""?0:$("#demandSelected").val(),
				priorityCheck	: $("#siPriority_1").is(":checked"),
				experiencia		: experience,
				req				: request,
				residencia		: residence,
				lenguaje		: lenguage,
				viajar			: $("#siTravel_1").is(":checked")
			};
		//alert(Object.toJSON(jsonObject));
		/***/
		var serverResponse = ajaxJSONtoPOST(Object.toJSON(jsonObject), serviceDemand);
		if(serverResponse.Flag=="true") {
			if(tipo=="1") {
				alert("Solicitud registrada correctamente");
				genericFilters("fDemand",1,0,0);
				$("#demandSelected").val(serverResponse.idGen);
			} else{
				alert("Solicitud actualizada correctamente");
			}
		} else {
			alert(serverResponse.Error);
		}
		/***/
	} 
}

function fieldsAreValids() {
	SalesPerson = $("#sdSalesperson_1 .lblcombo option:selected").val();
	Client		= $("#sdClient_1 .lblcombo option:selected").val();
	Contact 	= $("#sdContact_1 .lblcombo option:selected").val();	
	Vacancy 	= $("#siVacancy_1").val();								
	Profile 	= $("#siProfile_1").val();								
	Duration	= $("#siDuration_1").val();							
	Rate 		= $("#siTarifa_1").val();							
	cDate		= $("#siDate_1").val();							
	noVacancy 	= $("#siNoVacancy_1").val();	
	experience	= $("#siExperience_1").val();
	request		= $("#siPO_1").val();
	residence	= $("#siResidence_1").val();
	lenguage	= $("#siLenguage_1").val();
	
	//alert(SalesPerson +"|"+ Client +"|"+ Contact +"|"+ Vacancy +"|"+ Profile +"|"+ Duration +"|"+ Rate +"|"+ cDate +"|"+ noVacancy + "|" + residence + "|" +request + "|" + experience + "|" + lenguage);
	var dataIncomplete = new Array();
	if(!validateFields(SalesPerson,6))	{ dataIncomplete.push("Vendedor");		}
	if(!validateFields(Client, 6))		{ dataIncomplete.push("Cliente"); 		}
	if(!validateFields(Contact, 6))		{ dataIncomplete.push("Contacto"); 		}
	if(!validateFields(Vacancy, 1))		{ dataIncomplete.push("Vacante"); 		}
	if(!validateFields(cDate,5))		{ dataIncomplete.push("Fecha inicio");	}
	if(!validateFields(Duration, 4))	{ dataIncomplete.push("Duracion");		}
	if(!validateFields(Rate, 4))		{ dataIncomplete.push("Tarifa");    	}
	if(!validateFields(Profile, 1))		{ dataIncomplete.push("Perfil"); 		}
	if(!validateFields(noVacancy, 4))	{ dataIncomplete.push("No. vacantes");	}
	if(!validateFields(experience, 1))	{ dataIncomplete.push("Experiencia");	}
	if(!validateFields(request, 1))		{ dataIncomplete.push("Request");		}
	if(!validateFields(residence, 1))	{ dataIncomplete.push("Residencia");	}
	if(!validateFields(lenguage, 1))	{ dataIncomplete.push("Lenguaje");		}
	
	if(dataIncomplete.length > 0){
		var msg = "";
		for(var idx=0; idx<dataIncomplete.length; idx++) {
			if(idx == dataIncomplete.length-1){
				if(dataIncomplete[idx]!="") {
					msg+=dataIncomplete[idx]+"";
				}
			} else
				if(dataIncomplete[idx]!="") {
				msg+=dataIncomplete[idx]+", ";
			}
		}
		alert("Favor de llenar los campos faltantes: "+msg);
		return false;
	} else {
		return true;
	}
}

function modifyDemand(idDemand) {
	//alert(idDemand);
	loadDemandTab(0);
	$("#demandSelected").val(idDemand);
	foundDemands(2);
	$("#modifyDemandTbl").css("visibility", "visible");
}